<template>
    <div id="myCoupons">
        <div>
        <van-nav-bar
                title="我的优惠券"
                left-text="返回"
                left-arrow
                :fixed=true
                :border=true
                @click-left="onClickLeft"
        />
    </div>

        <div>

            <van-coupon-list
                    :coupons="coupons"
                    :chosen-coupon="chosenCoupon"
                    :disabled-coupons="disabledCoupons"
                    @change="onChange"
                    @exchange="onExchange"
                    close-button-text="去使用"
            />
        </div>

    </div>

</template>


<script>
    import {delcoup, getAllCoup} from "@/api/api"


    let tempList1 =[];
    let tempList2 =[];
    export default {
        created() {
            var time = new Date(new Date().getTime());
            var year = time.getFullYear();
            var month = time.getMonth()+1;
            var day = time.getDate();
            var hour = time.getHours();
            var minute = time.getMinutes();
            var second = time.getSeconds();
            this.nowTime=year+'-'+(month<10?'0'+month:month)+'-'+(day<10?'0'+day:day)+' '+(hour<10?'0'+hour:hour)+':'+(minute<10?'0'+minute:minute)+':'+(second<10?'0'+second:second)
            tempList1.length=0;
            tempList2.length=0;
            getAllCoup().then((response) => {
                let coup=response.data.data.coupons
                for (let i = 0; i <coup.length ; i++) {
                    const coupon = {
                        available: "",
                        condition: "",
                        reason: '',
                        value:"",
                        name: "",
                        startAt:"",
                        endAt: "",
                        valueDesc:"",
                        unitDesc: '',
                    };
                    console.log(coup[i].activeId);
                    if(coup[i].type==2){
                        console.log("111")
                        coupon.condition="满"+coup[i].withAmount+"可用";
                        coupon.valueDesc = (coup[i].usedAmount)*0.1;
                        coupon.unitDesc='折';
                        coupon.name="折扣券";
                        coupon.startAt= new Date(coup[i].startTime).getTime();
                        coupon.endAt=new Date(coup[i].endTime).getTime();

                    }
                    if(coup[i].type==1)
                    {
                        console.log("222")
                        coupon.valueDesc = coup[i].usedAmount;
                        coupon.unitDesc='元';
                        coupon.name="满减券";
                        coupon.condition="满"+coup[i].withAmount+"可用";
                        coupon.valueDesc = (coup[i].usedAmount);
                        coupon.startAt= new Date(coup[i].startTime).getTime();
                        this.coupStartTime=new Date(coup[i].endTime).getTime();
                        coupon.endAt=new Date(coup[i].endTime).getTime();
                        this.coupEndTime=new Date(coup[i].endTime).getTime();

                    }
                    if (coup[i].status==='1'&&this.nowTime<this.coupEndTime&&this.nowTime>=this.coupStartTime){
                        tempList1.push(coupon);
                    }else{
                        tempList2.push(coupon);
                    }
                }
                    // if (coup[i].status==='1'){
                    //     tempList1.push(coupon);
                    // }else{
                    //     tempList2.push(coupon);
                    // }

                // }
            });
        },
        data() {
            return {
                chosenCoupon: -1,
                coupons:tempList1,
                disabledCoupons:tempList2,
                nowTime:"",
                coupEndTime:"",
                coupStartTime:""

            };
        },
        methods: {
            onChange(index) {
                this.showList = false;
                this.chosenCoupon = index;
            },
            onExchange(code) {
                this.coupons.push(tempList1);
            },
            onClickLeft() { // 点击左边
                this.$router.back();  // 返回上一个路由
            },
        },
    }
</script>

<style scoped>

    #myCoupons {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #f5f5f5;
        z-index: 9999;
    }

</style>